package com.xy.annotaion.handler.kotlin.spider

import com.xy.annotaion.handler.kotlin.p
import org.jsoup.Jsoup
import java.io.FileOutputStream

fun main() {
    //100MB cache http request data
    val maxBytes = 1024 * 1024 * 100
    val file = FileOutputStream("forum.mp4")
    var homeUrl = "https://flv5.bn.netease.com"
    val videoPhaseList =
        "https://flv5.bn.netease.com/66f1d5ee40a3fd7dc70bd0033d4485a77434281857bfc08a3b8fca92eb56dcc1dcec02b5fd3bb6048961b2414da0dc7be2f6ca22e361c9d241b23e42e32dffd6197eb0cc29cce58962abb3a27647f587e246f0a4d99188f6213835d50b4ae6ca7c0b21e423f29e3c642448434943f3ff4bf731d531212eb5a37aeb9b383d095b44e52c7f34b29d5f41438117c970ebde484c4315460c0ded.m3u8"
    //jsoup 下载文件默认1MB数据，需要设置才能满足要求
    val videoListOriginData =
        Jsoup.connect(videoPhaseList).ignoreContentType(true).maxBodySize(maxBytes).execute().body()

    p(videoListOriginData)
    val originDataList = videoListOriginData.split("\n")
    for (i in originDataList) {
        p(i);
        if (i?.isNotBlank() && !i.startsWith("#")) {
            val videoPhaseData =
                Jsoup.connect(homeUrl + i).ignoreContentType(true).maxBodySize(maxBytes).execute().bodyAsBytes();
            file.write(videoPhaseData);
        }
    }
    file.flush();
    file.close();

}
